package com.order.controller;

import com.usian.order.OrderUser;
import com.order.service.OrderService;
import com.order.service.UserService;
import com.usian.order.Order;
import com.usian.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/app/order/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;
    @GetMapping("/{id}/{status}")
    public ResultVo getOrder(@PathVariable Integer id, @PathVariable Integer status) {
        // 参数验证
        if (id == null || status == null) {
            return ResultVo.error("Invalid parameters");
        }

        // 根据用户ID从订单服务中选择订单
        // status为0时，返回出行定制订单，为1时，返回咨询服务订单
        status = status == 0 ? 1 : 2;

        List<Order> list = orderService.selectTakeOrder(id, status);

        // 初始化 userList
        List<OrderUser> orderUserList = new ArrayList<>();

        // 检查订单列表是否不为空
        if (list != null) {
            // 遍历订单列表
            for (Order order : list) {
                // 根据订单中的用户ID从用户服务中选择用户
                OrderUser orderUser = userService.selectUserById(order.getUserId());

                // 检查用户是否不为空
                if (orderUser != null) {
                    orderUser.setOrderStatus(order.getOrderStatus());
                    // 将找到的用户添加到用户列表中
                    orderUserList.add(orderUser);
                }
            }
        } else {
            // 如果订单列表为空，返回错误的ResultVo
            return ResultVo.error();
        }

        // 返回包含用户列表的ResultVo
        return ResultVo.ok(orderUserList);
    }
}
